package emf2gwtbridge.generators;

import emf2gwtbridge.*;
import org.eclipse.emf.codegen.ecore.genmodel.*;

public class ServiceLocator
{
  protected static String nl;
  public static synchronized ServiceLocator create(String lineSeparator)
  {
    nl = lineSeparator;
    ServiceLocator result = new ServiceLocator();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "package ";
  protected final String TEXT_2 = ";";
  protected final String TEXT_3 = NL + "public class ";
  protected final String TEXT_4 = " implements ";
  protected final String TEXT_5 = " " + NL + "{" + NL + "  @Override" + NL + "  public Object getInstance(Class<?> clazz) {" + NL + "    try {" + NL + "      return clazz.newInstance();" + NL + "    } catch (InstantiationException e) {" + NL + "      throw new RuntimeException(e);" + NL + "    } catch (IllegalAccessException e) {" + NL + "      throw new RuntimeException(e);" + NL + "    }" + NL + "  }" + NL + "}";
  protected final String TEXT_6 = NL;

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    GenPackage genPackage = (GenPackage)argument; GenModel genModel = genPackage.getGenModel();
    stringBuffer.append(TEXT_1);
    stringBuffer.append(GeneratorUtils.getServiceLocatorPackageName(genPackage));
    stringBuffer.append(TEXT_2);
    genModel.markImportLocation(stringBuffer, genPackage);
    stringBuffer.append(TEXT_3);
    stringBuffer.append(GeneratorUtils.getServiceLocatorClassName(genPackage));
    stringBuffer.append(TEXT_4);
    stringBuffer.append(genModel.getImportedName("com.google.web.bindery.requestfactory.shared.ServiceLocator"));
    stringBuffer.append(TEXT_5);
    genModel.emitSortedImports();
    stringBuffer.append(TEXT_6);
    return stringBuffer.toString();
  }
}
